var config = require('../config.js');
function Get(url, data, cb) {
    this.isLogin(url);
    uni.showNavigationBarLoading();
    uni.request({
        method: 'GET',
        url: config.BASE_URL + url,
        data: data,
        header: {
            'U-TOKEN': uni.getStorageSync('user') ? uni.getStorageSync('user').token : url
        },
        success: (res) => {
            if (res.code == 401) {
                uni.setStorageSync('user', null);
            }
            if (typeof cb == 'function') {
                cb(res.data, '');
            }
            uni.hideNavigationBarLoading();
        },
        fail: (err) => {
            if (typeof cb == 'function') {
                cb(null, err.errMsg);
            }
            uni.hideNavigationBarLoading();
        }
    });
}
function Post(url, data, cb) {
    this.isLogin(url);
    uni.request({
        method: 'POST',
        url: config.BASE_URL + url,
        data: data,
        header: {
            'U-TOKEN': uni.getStorageSync('user') ? uni.getStorageSync('user').token : url
        },
        success: (res) => {
            if (res.code == 401) {
                uni.setStorageSync('user', null);
            }
            if (typeof cb == 'function') {
                cb(res.data, '');
            }
        },
        fail: (err) => {
            if (typeof cb == 'function') {
                cb(null, err.errMsg);
            }
        }
    });
}
function PutForm(url, data, cb) {
    this.isLogin(url);
    uni.request({
        method: 'PUT',
        url: config.BASE_URL + url,
        data: data,
        header: {
            'U-TOKEN': uni.getStorageSync('user') ? uni.getStorageSync('user').token : url,
            'content-type': 'application/x-www-form-urlencoded'
        },
        success: (res) => {
            if (res.code == 401) {
                uni.setStorageSync('user', null);
            }
            if (typeof cb == 'function') {
                cb(res.data, '');
            }
        },
        fail: (err) => {
            if (typeof cb == 'function') {
                cb(null, err.errMsg);
            }
        }
    });
}
function Put(url, data, cb) {
    this.isLogin(url);
    uni.request({
        method: 'PUT',
        url: config.BASE_URL + url,
        data: data,
        header: {
            'U-TOKEN': uni.getStorageSync('user') ? uni.getStorageSync('user').token : url,
            'content-type': 'application/json'
        },
        success: (res) => {
            if (res.code == 401) {
                uni.setStorageSync('user', null);
            }
            if (typeof cb == 'function') {
                cb(res.data, '');
            }
        },
        fail: (err) => {
            if (typeof cb == 'function') {
                cb(null, err.errMsg);
            }
        }
    });
}
function Upload(url, file, data, cb) {
    this.isLogin(url);
    uni.uploadFile({
        url: config.BASE_URL + url,
        filePath: file,
        method: 'POST',
        name: 'file',
        header: {
            'U-TOKEN': uni.getStorageSync('user') ? uni.getStorageSync('user').token : url,
            'Content-Type': 'multipart/form-data'
        },
        formData: data,
        success: (res) => {
            if (res.code == 401) {
                uni.setStorageSync('user', null);
            }
            if (typeof res.data == 'string') {
                if (typeof cb == 'function') {
                    cb(JSON.parse(res.data), '');
                }
            } else {
                if (typeof cb == 'function') {
                    cb(res.data, '');
                }
            }
        },
        fail: (err) => {
            if (typeof cb == 'function') {
                cb(null, err.errMsg);
            }
        }
    });
}
function putOpenid(code, cb) {
    uni.request({
        method: 'GET',
        url: config.BASE_URL + config.clubApi.putOpenid,
        data: {
            code: code
        },
        header: {
            'U-TOKEN': uni.getStorageSync('user') ? uni.getStorageSync('user').token : config.clubApi.putOpenid
        },
        success: (res) => {
            if (res.code == 401) {
                uni.setStorageSync('user', null);
            }
            if (typeof cb == 'function') {
                cb(res.data, '');
            }
            uni.hideNavigationBarLoading();
        },
        fail: (err) => {
            if (typeof cb == 'function') {
                cb(null, err.errMsg);
            }
            uni.hideNavigationBarLoading();
        }
    });
}
function isLogin(url) {
    let permitURLs = [
        '/userapp/getProductTypes',
        '/userapp/getMealTypes',
        '/userapp/getShopList',
        '/userapp/getShopInfo',
        '/userapp/getShopAllList',
        '/userapp/getProductList',
        '/userapp/getMealList',
        '/userapp/getProductInfo',
        '/userapp/getTeacherList',
        '/userapp/getTeacherInfo',
        '/userapp/getCourseList',
        '/userapp/getCourseInfo',
        '/userapp/getPhone',
        '/userapp/wxUser',
        '/auth/getPhoneCode',
        '/auth/loginByCode',
        '/auth/getopenid',
        '/auth/getWXInterface',
        '/auth/login',
        '/auth/forget'
    ];
    if (permitURLs.indexOf(url) >= 0) {
        return true;
    } else {
        if (!uni.getStorageSync('user') || !uni.getStorageSync('user').token) {
            return uni.switchTab({
                url: '/pages/user/index'
            });
        }
    }
}
module.exports = {
    httpGet: Get,
    httpPost: Post,
    httpPut: Put,
    httpPutForm: PutForm,
    httpUpload: Upload,
    putOpenid: putOpenid,
    isLogin: isLogin
};
